﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TurboTemplate
{
    public partial class TemplateEditorDialog : Form
    {

        private List<TransformTemplate> m_Templates;
        private TransformTemplate m_CurrentTemplate;
        
        public TemplateEditorDialog()
        {
            InitializeComponent();
        }

        private void TemplateEditorDialog_FormClosing(object sender, FormClosingEventArgs e)
        {
            GeneratorConfiguration.Instance.Syntax = (string)cboSyntax.SelectedItem;
        }

        private void TemplateEditorDialog_Load(object sender, EventArgs e)
        {
            cboSyntax.SelectedItem = GeneratorConfiguration.Instance.Syntax;
            if (cboSyntax.SelectedItem == null)
            {
                cboSyntax.SelectedIndex = 0;
            }
            
            m_Templates = TemplateHelper.GetTemplates();

            lstTemplates.DisplayMember = "Name";
            lstTemplates.DataSource = m_Templates;

            UpdateUI();
        }

        private void lstTemplates_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstTemplates.BindingContext[m_Templates].IsBindingSuspended)
                return;

            // Load the template
            var selectedTemplate = lstTemplates.SelectedItem as TransformTemplate;
            if (selectedTemplate == null)
                return;

            Load_Template(selectedTemplate);
            UpdateUI();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (m_CurrentTemplate != null)
                TemplateHelper.Save(m_CurrentTemplate, m_CurrentTemplate.FileName);

            DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }

        private void UpdateUI()
        {
            tblTemplateEditLayout.Enabled = (lstTemplates.SelectedItem != null);
            lnkDelete.Enabled = (lstTemplates.SelectedItem != null);
        }

        private void lnkAdd_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            using (NewTemplateDialog f = new NewTemplateDialog())
            {
                if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    TransformTemplate newTemplate = new TransformTemplate();
                    newTemplate.Name = f.TemplateName;
                    newTemplate.Template = "";
                    newTemplate.Syntax = GeneratorConfiguration.Instance.Syntax;

                    TemplateHelper.Save(newTemplate, TemplateHelper.GetFileNameForNewTemplate(newTemplate));

                    lstTemplates.BindingContext[m_Templates].SuspendBinding();
                    m_Templates.Add(newTemplate);
                    lstTemplates.BindingContext[m_Templates].ResumeBinding();
                    lstTemplates.SelectedItem = newTemplate;
                }
            }
        }

        private void lnkDelete_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            var selectedTemplate = lstTemplates.SelectedItem as TransformTemplate;
            if (selectedTemplate == null)
                return;

            var msg = MessageBox.Show("Are you sure you want to delete the template '" + selectedTemplate.Name + "'?", 
                "Delete Template",
                MessageBoxButtons.YesNo, 
                MessageBoxIcon.Question);

            if (msg == System.Windows.Forms.DialogResult.Yes)
            {
                lstTemplates.BindingContext[m_Templates].SuspendBinding();
                m_Templates.Remove(selectedTemplate);
                TemplateHelper.DeleteTemplate(selectedTemplate);
                lstTemplates.BindingContext[m_Templates].ResumeBinding();
                lstTemplates.Refresh();
            }

            UpdateUI();
        }

        private void lstTemplates_DoubleClick(object sender, EventArgs e)
        {

            var p = lstTemplates.PointToClient(Cursor.Position);
            var index = lstTemplates.IndexFromPoint(p);

            if (index > -1)
            {
                var item = lstTemplates.Items[index] as TransformTemplate;
                using (NewTemplateDialog f = new NewTemplateDialog(item.Name))
                {
                    if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        lstTemplates.BindingContext[m_Templates].SuspendBinding();
                        item.Name = f.TemplateName;
                        TemplateHelper.Save(item, item.FileName);
                        lstTemplates.BindingContext[m_Templates].ResumeBinding();
                        lstTemplates.SelectedItem = item;
                    }
                }
            }

        }

        private void Load_Template(TransformTemplate t)
        {
            if (m_CurrentTemplate != null)
                TemplateHelper.Save(m_CurrentTemplate, m_CurrentTemplate.FileName);

            m_CurrentTemplate = t;
            cboSyntax.SelectedItem = t.Syntax;

            txtTemplate.Text = t.Template;
            txtTemplate.Document.RequestUpdate(new ICSharpCode.TextEditor.TextAreaUpdate(ICSharpCode.TextEditor.TextAreaUpdateType.WholeTextArea));

            var highlightingStrategy = (t.Syntax.ToLower() == "vb.net" ? "VBNET" : "C#");
            txtTemplate.SetHighlighting(highlightingStrategy);

        }

        private void cboSyntax_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (m_CurrentTemplate != null)
            {
                m_CurrentTemplate.Syntax = (string)cboSyntax.SelectedItem;
                var highlightingStrategy = (m_CurrentTemplate.Syntax.ToLower() == "vb.net" ? "VBNET" : "C#");
                txtTemplate.SetHighlighting(highlightingStrategy);
            }
        }

        private void txtTemplate_TextChanged(object sender, EventArgs e)
        {
            if (m_CurrentTemplate != null)
            {
                m_CurrentTemplate.Template = txtTemplate.Text;
            }
        }
    }
}
